package com.java2;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *  * 实现TCP的网络编程
 *  * 例题3：从客户端发送文件给服务端，服务端保存到本地。并返回“发送成功”给客户端。
 *  * 并关闭相应的连接。
 */
public class TCPTest3 {

    //应该使用try-catch-finally来处理异常
    @Test
    public void client() throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(), 9090);

        OutputStream os = socket.getOutputStream();
        FileInputStream fis = new FileInputStream(new File("01.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1){
            os.write(buffer,0,len);
        }
        //关闭数据的输出
        socket.shutdownOutput();
        //5.接收来自于服务器端的数据，并显示到控制台上
        InputStream is = socket.getInputStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer1 = new byte[1024];
        int len1;
        while ((len1 = is.read(buffer1)) !=-1){
            baos.write(buffer1,0,len1);
        }
        System.out.println(baos.toString());
        baos.close();
        is.close();
        fis.close();
        os.close();
    }

    @Test
    public void server() throws IOException{

        ServerSocket serverSocket = new ServerSocket(9090);
        Socket socket = serverSocket.accept();
        InputStream is = socket.getInputStream();

        FileOutputStream fos = new FileOutputStream(new File("03.jpg"));

        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
        System.out.println("图片传输完成!");
        //6.服务器端给予客户端反馈
        OutputStream os = socket.getOutputStream();
        os.write("你好，美女，照片我已收到，非常漂亮！".getBytes());
        os.close();
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }
}
